Imports System.Data.OracleClient

Public Class KurumsalFaturaSikayetiFaturaDetay
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object
    Protected WithEvents dgPageTwo1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents dgPageTwo2 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents dgPageTwo3 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents dgPageOne1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents dgPageOne2 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lbl1 As System.Web.UI.WebControls.Label
    Protected WithEvents lbl2 As System.Web.UI.WebControls.Label
    Protected WithEvents lbl3 As System.Web.UI.WebControls.Label
    Protected WithEvents dgPageOne3 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lblWarning1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblWarning2 As System.Web.UI.WebControls.Label
    Protected WithEvents lblWarning3 As System.Web.UI.WebControls.Label


    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim msisdn As String = IIf(Request.QueryString("msisdn") Is Nothing, "", Request.QueryString("msisdn"))
        msisdn = msisdn.Trim()

        Dim cutOff As String = billCycle(-1, persistent.WebOnline.GetCustomerID(msisdn, New String(String.Empty)))


        Dim dt As DataTable = DisplayFaturaDetails(msisdn, -1)
        If dt.Rows.Count = 0 Then
            dgPageOne1.Visible = False
            dgPageTwo1.Visible = False
            lbl1.Text = CDate(cutOff).AddMonths(-1) & " - " & CDate(cutOff).AddDays(-1)
            lblWarning1.Visible = True

        Else
            lbl1.Text = CStr(dt.Rows(0)("INV_PERIOD")).Replace("/", ".")


        End If
        dt.DefaultView().RowFilter = "PAGE_NO=1"
        dgPageOne1.DataSource = dt.DefaultView()
        dgPageOne1.DataBind()

        dt.DefaultView().RowFilter = "PAGE_NO=2"
        dgPageTwo1.DataSource = dt.DefaultView()
        dgPageTwo1.DataBind()


        dt = DisplayFaturaDetails(msisdn, -2)
        If dt.Rows.Count = 0 Then
            dgPageOne2.Visible = False
            dgPageTwo2.Visible = False
            lbl2.Text = CDate(cutOff).AddMonths(-2) & " - " & CDate(cutOff).AddMonths(-1).AddDays(-1)
            lblWarning2.Visible = True
        
        Else
            lbl2.Text = CStr(dt.Rows(0)("INV_PERIOD")).Replace("/", ".")

        End If
        dt.DefaultView().RowFilter = "PAGE_NO=1"
        dgPageOne2.DataSource = dt.DefaultView()
        dgPageOne2.DataBind()

        dt.DefaultView().RowFilter = "PAGE_NO=2"
        dgPageTwo2.DataSource = dt.DefaultView()
        dgPageTwo2.DataBind()


        dt = DisplayFaturaDetails(msisdn, -3)
        If dt.Rows.Count = 0 Then
            dgPageOne3.Visible = False
            dgPageTwo3.Visible = False
            lbl3.Text = CDate(cutOff).AddMonths(-3) & " - " & CDate(cutOff).AddMonths(-3).AddDays(-1)
            lblWarning3.Visible = True
         
        Else
            lbl3.Text = CStr(dt.Rows(0)("INV_PERIOD")).Replace("/", ".")

        End If
        dt.DefaultView().RowFilter = "PAGE_NO=1"
        dgPageOne3.DataSource = dt.DefaultView()
        dgPageOne3.DataBind()

        dt.DefaultView().RowFilter = "PAGE_NO=2"
        dgPageTwo3.DataSource = dt.DefaultView()
        dgPageTwo3.DataBind()

    End Sub


    Private Function DisplayFaturaDetails(ByVal msisdn As String, ByVal donem As Integer) As DataTable
        Dim cutOff As String = billCycle(donem, persistent.WebOnline.GetCustomerID(msisdn, New String(String.Empty)))
        Dim cutOffLast As String = billCycle(donem - 1, persistent.WebOnline.GetCustomerID(msisdn, New String(String.Empty)))
        'Dim cutOff As String = "26.12.2013"
        Dim con As New OracleConnection(ConfigurationSettings.AppSettings("OraConnStr"))
        Dim cmd As New OracleCommand("eu_bpm.queryvf.get_invoice_data")
        Dim ds As New DataSet
        Dim da As New Data.OracleClient.OracleDataAdapter(cmd)

        con.Open()
        cmd.Connection = con
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("p_msisdn", OracleType.NVarChar).Value = msisdn '"5064995820"
        'cmd.Parameters.Add("p_msisdn", OracleType.NVarChar).Value = "5064995820"
        'Dim x As String = CDate(cutOff).AddMonths(-1).AddDays(1) & " - " & CDate(cutOff)
        'Dim xz As String = CDate(cutOffLast).AddDays(-1) & " - " & CDate(cutOff).AddDays(-1)
        cmd.Parameters.Add("p_inv_period", OracleType.NVarChar).Value = CDate(cutOffLast) & " - " & CDate(cutOff).AddDays(-1) '"'27.11.2013 - 26.12.2013" '"27/11/2013 - 26/12/2013"
        cmd.Parameters.Add("p_invoice", OracleType.Cursor).Direction = ParameterDirection.Output
        da.Fill(ds)
        con.Close()
        For Each r As DataRow In ds.Tables(0).Rows
            If IsDBNull(r("bold")) Then
                r("bold") = ""
            End If
        Next

        Return ds.Tables(0)

    End Function

    Private Function billCycle(ByVal donem As Integer, ByVal customerID As String) As String
        Return persistent.myDBConn.ORAScalar("select EU_BILL.AVEA_BC_UTIL.NEXTCUTOFF_FROM_PERIOD(add_months(sysdate, " & donem & "),   eu_bill.get_period_from_billcycle (eu_bill.fn_util_getbillcycle(" & customerID & "))) from dual ")
    End Function
End Class
